﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace InfoPathExtractionRules
{
    public class EventLog
    {
        // Separator characters used in the postback request string body
        private const char EventLogSeparator = ' ';
        private const char EventLogItemSeparator = ';';

        public class EventLogInfo
        {
            private string[] items;

            public static class Index
            {
                public const int
                    PostbackCounter = 1,
                    EditingSessionId = 2,
                    SolutionId = 3,
                    CanaryValue = 18,
                    NumberOfItems = 19
                ;
            }

            public EventLogInfo(string eventLogInfo)
            {
                items = eventLogInfo.Split(EventLogItemSeparator);
                int numberOfItems = items.Length;
                int numberOfExpectedItems = Index.NumberOfItems;
                if (numberOfItems != numberOfExpectedItems)
                {
                    string error = string.Format("Event log info has {0} items, expected {1}", numberOfItems, numberOfExpectedItems);
                    Trace.TraceError(error);
                }
            }

            public int PostbackCounter
            {
                get { return int.Parse(items[Index.PostbackCounter]); }
                set { items[Index.PostbackCounter] = value.ToString(); }
            }

            public string EditingSessionId
            {
                get { return items[Index.EditingSessionId]; }
                set { items[Index.EditingSessionId] = value; }
            }

            public string SolutionId
            {
                get { return items[Index.SolutionId]; }
                set { items[Index.SolutionId] = value; }
            }

            public string Canary
            {
                get { return items[Index.CanaryValue]; }
                set { items[Index.CanaryValue] = value; }
            }

            public override string ToString()
            {
                return String.Join(EventLogItemSeparator.ToString(), items);
            }
        }

        private string[] eventLogParts;
        public EventLog(string eventLog)
        {
            if (!eventLog.EndsWith(" "))
            {
                Trace.TraceWarning("An event log does not end with a space character.");
            }
            if (eventLog.EndsWith("  "))
            {
                Trace.TraceWarning("An event log ends with more than one space character.");
            }

            eventLogParts = eventLog.Split(EventLogSeparator);
            if (eventLogParts == null || eventLogParts.Length <= 0)
            {
                throw new ArgumentException("eventLogParts null or empty");
            }

            eventLogInfo = new EventLogInfo(eventLogParts[0]);
        }

        private EventLogInfo _eventLogInfo = null;
        public EventLogInfo eventLogInfo
        {
            get
            {
                if (_eventLogInfo == null)
                    _eventLogInfo = new EventLogInfo(eventLogParts[0]);
                return _eventLogInfo;
            }
            set { _eventLogInfo = value; }
        }

        public override string ToString()
        {
            eventLogParts[0] = _eventLogInfo.ToString();
            return String.Join(EventLogSeparator.ToString(), eventLogParts);
        }
    }
}
